package com.oldroid.apps.getit.ui;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.oldroid.apps.getit.R;
import com.oldroid.apps.getit.util.AnalyticsUtils;


/**
 * Search Activity
 * @author Jervis Muindi
 *
 */
public class SearchActivity extends BaseActivity{
	private static final String TAG = "SearchActivity";
	private Activity mActivity = this; 
	static TextView details;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		AnalyticsUtils.getInstance(this).trackPageView("/Search");
		setContentView(R.layout.activity_search);
		getActivityHelper().setupActionBar(null, 0);
		
		
		// See if it's a search query
		
		Intent intent = getIntent();
		if(intent.getAction().equals(Intent.ACTION_SEARCH)){
			String query = intent.getStringExtra(SearchManager.QUERY);
			doMySearch(query); 
		} else if(intent.getAction().equals(Intent.ACTION_VIEW)){ // search suggestion intent
			
			Uri data = intent.getData();
			String query =  data.getLastPathSegment().toLowerCase();
			
			searchByID(query); 
			openByID(data);
		}
		
		
		
	}
	
	
	/**
	 * Opens the given file. 
	 * @param data
	 */
	private void openByID(Uri data){
		
		String query = data.getLastPathSegment().toLowerCase(); 
		
		FragmentManager fm = getSupportFragmentManager();
		SearchFragment searchFragment = (SearchFragment) fm.findFragmentById(R.id.fragment_search); 
		searchFragment.openByID(query);
		
		
	}
	
	private void searchByID(String query){
		
		FragmentManager fm = getSupportFragmentManager();
		SearchFragment searchFragment = (SearchFragment) fm.findFragmentById(R.id.fragment_search); 
		searchFragment.searchByID(query);
		
		
	}
	
	private void doMySearch(String query){
		
		FragmentManager fm = getSupportFragmentManager();
		SearchFragment searchFragment = (SearchFragment) fm.findFragmentById(R.id.fragment_search); 
		
		searchFragment.search(query); 
		 
		//Toast.makeText(this, "Search Received. Performing SEARCH...", Toast.LENGTH_LONG).show(); 

	}

//	
//
//	/**
//	 * Shows the results of the search on to the list view
//	 * @param cursor cursor containing the search results. 
//	 */
//	private void showResult(Cursor cursor){
//		
//		String[] from = {};
//		int[] to = {};
//		
//		SimpleCursorAdapter adapter = new SimpleCursorAdapter(mActivity, R.layout.list_item_row, cursor, from, to);
//		
//		FragmentManager fm = getSupportFragmentManager();
//		SearchFragment search = (SearchFragment) fm.findFragmentById(R.id.fragment_search);
//		
//		
//		
//		
//		setListAdapter(adapter);
//		
//		
//		
//		int count = cursor.getCount(); 
//		say("Search Returned " +  count + " Results."); 
//	}
//	
//	
//	private void say(String msg){
//		Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT);
//	}
//	
//	/**
//	 * Searches the Downloads Databse for downloaded files that match the given string query  
//	 * @param query
//	 */
//	private void search(String query){
//		
//		String [] projection = null ; // get all the columns
//		String selection = Downloads.FILENAME + " LIKE '%" + query +"%'"; //Like statemnts tells SQL to do partial matching
//		String[] selectionArgs = null; // no binding the values here  
//		String sortOrder = Downloads.FILENAME + " ASC"; // ascending order 
//		
//		Cursor c = mActivity.managedQuery(Downloads.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
//	
//		showResult(c);
//		
//	}
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		getActivityHelper().setupHomeActivity();
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.refresh_menu_items, menu);
		super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.menu_refresh){
			// need to implement refresh probably not
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	

}
